package com.ruben.simplescaffold.utils;

import cn.hutool.core.exceptions.ExceptionUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步工具类
 *
 * @author <achao1441470436@gmail.com>
 * @date 2021/5/7 0007 10:31
 */
public class FutureUtils {
    private FutureUtils() {
    }

    /**
     * 等待结束
     *
     * @param userFuture 任务
     * @author <achao1441470436@gmail.com>
     * @date 2021/5/7 0007 10:39
     */
    public static void wait(CompletableFuture<?>... userFuture) {
        try {
            CompletableFuture.allOf(userFuture).get();
        } catch (InterruptedException | ExecutionException e) {
            throw ExceptionUtil.wrapRuntime(e);
        }
    }

    /**
     * 获取结果
     *
     * @param future 任务
     * @return T
     * @author <achao1441470436@gmail.com>
     * @date 2021/5/7 0007 10:39
     */
    public static <T> T get(CompletableFuture<T> future) {
        try {
            return future.get();
        } catch (InterruptedException | ExecutionException e) {
            throw ExceptionUtil.wrapRuntime(e);
        }
    }

}
